# Maintainer: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>

_realname=pdf2djvu
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.19
pkgrel=4
pkgdesc="Creates DjVu files from PDF files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://jwilk.net/software/pdf2djvu"
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-djvulibre"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "autoconf-archive"
             "${MINGW_PACKAGE_PREFIX}-autotools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose")
source=(https://github.com/jwilk/pdf2djvu/releases/download/${pkgver}/pdf2djvu-${pkgver}.tar.xz{,.asc}
        "001-cxx17-for-poppler.patch")
sha256sums=('eb45a480131594079f7fe84df30e4a5d0686f7a8049dc7084eebe22acc37aa9a'
            'SKIP'
            '4c50966b0fc05d78f6ec78b20df587fb5e1861f3ff326ee66c8baf10ec3cb39f')
validpgpkeys=('CDB5A1243ACDB63009AD07212D4EB3A6015475F5') # Jakub Wilk <jwilk@jwilk.net>
noextract=(pdf2djvu-${pkgver}.tar.xz)

prepare() {
  [[ -d ${_realname}-${pkgver} ]] && rm -rf ${_realname}-${pkgver}
  tar -xf pdf2djvu-${pkgver}.tar.xz || true
  cp ${srcdir}/${_realname}-${pkgver}/tests/test-antialias-off.tex \
     ${srcdir}/${_realname}-${pkgver}/tests/test-antialias-on.tex
  cd ${srcdir}/${_realname}-${pkgver}

  patch -p1 -i "${srcdir}/001-cxx17-for-poppler.patch"

  autoreconf -fvi
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  cp -rf ${_realname}-${pkgver} build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  # exiv2 depends on std::auto_ptr which got removed.
  # should be fixed in exiv2 0.28
  CXXFLAGS+=" -D_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR=1" \
  ./configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --with-libiconv-prefix=${MINGW_PREFIX}
  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR=${pkgdir} install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/doc/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
